package login;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Question loginServlet
 * @Date 2022/10/15 18:12
 * @Solution
 */
@WebServlet("/login")
public class loginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        if(username==null||password==null||username.equals("")||password.equals("")) {
            resp.getWriter().write("用户名或密码不完整");
            return;
        }
        if(!username.equals("zhangsan")||!password.equals("123")) {
            resp.getWriter().write("用户名或密码错误");
            return;
        }
        HttpSession session = req.getSession(true);
        session.setAttribute("username","zhangsan");
        session.setAttribute("password","123");
        session.setAttribute("num",1);
        resp.sendRedirect("index");
    }
}
